<?php


namespace api\modules\v1\controllers\system\base;

use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use common\models\base\BodyParts;
use services\base\BodyPartsServer;
use yii\web\HttpException;

class BodyPartsController extends OnAuthController
{
    public $modelClass = BodyParts::class;

    protected $optional = [];

    /**
     * @api {get} v1/system/base/body-parts/index 部位列表
     * @apiGroup 分组名称
     * @apiName 名称
     *
     * @apiParam {Number} type          类型【0部位列表、1业绩部位】
     * @apiParam {Number} page          页码
     * @apiParam {Number} pageSize      每页显示数量
     *
     * @apiSuccess {Number} code        状态码，200：请求成功
     * @apiSuccess {String} message     提示信息
     * @apiSuccess {Object} data        返回数据
     *
     */
    public function actionIndexPosition()
    {
        $params = Yii::$app->request->get();
        $params['type'] = $params['type'] ?? BodyParts::TYPE_NORMAL;

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        return  Yii::$app->services->bodyParts->positionList($page,$pageSize,$params);
    }

    /**
     * @api {get} v1/system/base/body-parts/father-list 父部位列表
     * @apiGroup 分组名称
     * @apiName 名称
     *
     * @apiParam {Number} type              类型【0部位列表、1业绩部位】
     *
     * @apiSuccess {Number} code            状态码，200：请求成功
     * @apiSuccess {String} message         提示信息
     * @apiSuccess {Object} data            返回数据
     *
     */
    public function actionFatherList()
    {
        $params = Yii::$app->request->get();
        $params['type'] = $params['type'] ?? BodyParts::TYPE_NORMAL;

        return  Yii::$app->services->bodyParts->fatherList($params);
    }

    /**
     * @api {post} v1/system/base/body-parts/create 新增部位
     * @apiGroup 分组名称
     * @apiName 名称
     *
     * @apiParam {String} body_parts_name       部位名
     * @apiParam {Number} pid                   父类id
     * @apiParam {Number} sort                  排序
     * @apiParam {Number} type                  类型【0部位列表、1业绩部位】
     *
     * @apiSuccess {Number} code                状态码，200：请求成功
     * @apiSuccess {String} message             提示信息
     * @apiSuccess {Object} data                返回数据
     *
     */
    public function actionCreatePosition()
    {
        $params = Yii::$app->request->post();
        $params['pid'] = intval($params['pid'] ?? 0);
        try {
            $res = Yii::$app->services->bodyParts->positionCreate($params);
        } catch (\Exception $exception) {
            throw new HttpException(500, $exception->getMessage());
        }
        return CommonHelper::api($res['code'],$res['message']);
    }

    /**
     * @api {post } v1/system/base/body-parts/update 更新部位
     * @apiGroup 分组名称
     * @apiName 名称
     *
     * @apiParam {Number} id                    部位ID
     * @apiParam {String} body_parts_name       部位名
     * @apiParam {Number} pid                   父类id
     * @apiParam {Number} sort                  排序
     * @apiParam {Number} status                状态【1启用、0禁用】
     *
     *
     * @apiSuccess {Number} code                状态码，200：请求成功
     * @apiSuccess {String} message             提示信息
     * @apiSuccess {Object} data                返回数据
     *
     */
    public function actionUpdatePosition()
    {
        $params = Yii::$app->request->post();
        $data = Yii::$app->services->bodyParts->positionUpdate($params);
        return $data;
    }

    public function actionViewPosition($id)
    {
        $data = parent::actionView($id);
        $data = $data->attributes;

        if ($data['pid'] == 0) {
            $data['pid_name'] = '';
        } else {
            $pidData = parent::actionView($data['pid']);
            $data['pid_name'] = $pidData['body_parts_name'];
        }
        return $data;
    }



    /**
     * @api {post } v1/system/base/body-parts/change-position 名称
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.0
     *
     * @apiDescription 接口描述
     *
     * @apiParam {Number} $id
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionChangePosition()
    {
        $params = Yii::$app->request->post();
        $model = parent::actionView($params['id']);
        $status = $model->status ? BodyParts::STATUS_INVALID : BodyParts::STATUS_ENABLE;
        $data = Yii::$app->services->bodyParts->deleteAndDisable($model->pid,$params['id'],$status);

        if ($data['code'] == 200) {
            $model->status = $status;

            if ($model->save()) {
                return CommonHelper::api(200,'success');
            }
            return CommonHelper::api(422,'保存失败');
        }
        return CommonHelper::api(422,$data['message']);
    }

    /**
     * @api {post} v1/system/base/body-parts/delete-position 名称
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.0
     *
     * @apiDescription 删除部分
     *
     * @apiParam {Number} $id
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionDeletePosition()
    {
        $params = Yii::$app->request->post();
        $model = parent::actionView($params['id']);
        $data = Yii::$app->services->bodyParts->deleteAndDisable($model->pid,$params['id'],BodyParts::STATUS_DELETE);

        if ($data['code'] == 200) {
            $model->status = BodyParts::STATUS_DELETE;

            if ($model->save()) {
                return CommonHelper::api(200,'删除成功');
            }
            return CommonHelper::api(422,'删除失败');
        }
        return CommonHelper::api(422,$data['message']);
    }


    /**
     * @api {post} v1/system/base/body-parts/update-status-position
     * @apiGroup 分组名称
     * @apiName 修改部位状态
     * @apiVersion 1.0.0
     *
     * @apiDescription 修改部位状态
     *
     * @apiParam {Number} $id
     * @apiParam {Number} $status
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionUpdateStatusPosition()
    {
        $id = Yii::$app->request->post('id', 0);
        $status = intval(Yii::$app->request->post('status'));
        if (!BodyParts::findOne($id)) {
            throw new HttpException(422, '修改失败，该部位不存在！');
        }
        $pid = (int) BodyParts::find()->where(['id' => $id])->select(['pid'])->scalar();
        $data = Yii::$app->services->bodyParts->deleteAndDisable($pid, $id,$status);
        if ($data['code'] == 200) {
            try {
                $res = BodyPartsServer::changeStatus($id, $status);
            } catch (\Exception $exception) {
                throw new HttpException(500, $exception->getMessage());
            }

            return $res;
        }

        throw new HttpException(422, $data['message']);
    }

    public function actionTrueDelete()
    {
        $id = Yii::$app->request->post('id');
        return Yii::$app->services->bodyParts->trueDelete($id);
    }


}
